package com.foreverwin.mes.lims.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.lims.dto.InactiveTimeDto;
import com.foreverwin.mes.lims.model.InactiveTimeItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.lims.service.InactiveTimeService;
import com.foreverwin.mes.lims.model.InactiveTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Max
 * @since 2020-03-09
 */
@Controller
@RequestMapping("/inactiveTimes")
public class InactiveTimeController {
    private final Logger logger = LoggerFactory.getLogger(InactiveTimeController.class);

    @Autowired
    public InactiveTimeService inactiveTimeService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getInactiveTimeById(@PathVariable String id) {
        InactiveTime result;
        try {
            result = inactiveTimeService.selectById(id);
        } catch (Exception e) {
            logger.error("getInactiveTimeById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询呆滞时间明细
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getInactiveTimeList(InactiveTime inactiveTime){
        List<InactiveTimeItem> result;
        try {
            result = inactiveTimeService.selectTimeSetList(inactiveTime);
        } catch (Exception e) {
            logger.error("getInactiveTimeList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }


    /**
     * 保存和修改公用
     * @param inactiveTimeDto  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult inactiveTimeSave(@RequestBody InactiveTimeDto inactiveTimeDto) {
        int count;
        try {
            count = inactiveTimeService.insertOrUpdateTimeSetList( inactiveTimeDto );
        } catch (Exception e) {
            logger.error("inactiveTimeSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : inactiveTimeDto);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult inactiveTimeDelete(@PathVariable("id") String id){
        int count;
        try {
            count = inactiveTimeService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("inactiveTimeDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = inactiveTimeService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("inactiveTimeBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}